package com.youmingxing.adpter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.InfoConfirmActivity;
import com.youmingxing.bean.AuditionDetail;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.ViewHolderUtils;
import com.youmingxing.view.DialogManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author evilyin(ChenZhixi)
 * @since 15/9/16
 */
public class SelectListAdapter extends BaseAdapter {

    private Context context;
    private List<AuditionDetail> list = new ArrayList<>();

    public SelectListAdapter(Context context, List<AuditionDetail> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item_audition_select, null);
        }
        final AuditionDetail thisAudition = list.get(position);

        ImageView image = ViewHolderUtils.getViewHolderView(convertView, R.id.category_img);
        TextView name = ViewHolderUtils.getViewHolderView(convertView, R.id.category_name);
        TextView title = ViewHolderUtils.getViewHolderView(convertView, R.id.category_title);
        TextView detail = ViewHolderUtils.getViewHolderView(convertView, R.id.category_detail);
        TextView desc = ViewHolderUtils.getViewHolderView(convertView, R.id.category_desc);
        TextView button = ViewHolderUtils.getViewHolderView(convertView, R.id.sign_button);

        switch (thisAudition.auditionStatus) { // 海选状态 1 未开始 2 海选中 3 海选结束未出结果 4 海选结束结果已出
            case 1:
                button.setText(R.string.not_started);
                button.setBackgroundColor(0xfff9d3d2);
                button.setOnClickListener(null);
            break;
            case 2:
                button.setText(R.string.sign);
                button.setBackgroundColor(0xfff94c4c);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogManager.showDialog(context, "正在验证报名资格");
                        AllStarApi.checkQualification(thisAudition.movieID, thisAudition.cateID, new
                                BaseJsonHandler(context) {
                            @Override
                            public void onSuccessWithData(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                int result = data.optInt("result");
                                if (result == 1) {
                                    Intent intent = new Intent(context, InfoConfirmActivity.class);
                                    intent.putExtra("movie_id", thisAudition.movieID);
                                    intent.putExtra("cate_id", thisAudition.cateID);
                                    intent.putExtra("audition_name", thisAudition.cate_name);
                                    context.startActivity(intent);
                                } else {
                                    showToast(data.optString("info"));
                                }
                            }
                        });
                    }
                });
            break;
            case 3:
            case 4:
                button.setText(R.string.ended);
                button.setBackgroundColor(0xffe8e8e8);
                button.setOnClickListener(null);
            break;
        }
        if (thisAudition.my_apply) {
            button.setText(R.string.my_apply);
            button.setBackgroundColor(context.getResources().getColor(R.color.red_global));
            button.setOnClickListener(null);
        }

        Glide.with(context).load(thisAudition.img)
                    .placeholder(R.drawable.default_1_1).error(R.drawable.default_1_1)
                    .crossFade().into(image);
        name.setText(thisAudition.cate_name);
        title.setText(thisAudition.title);
        detail.setText("名额: " + thisAudition.quota + " " + thisAudition.getGender() + " 年龄: " +
                "" + thisAudition.getAge());
        String descText = thisAudition.intro.replace("\\n", " ").replace("\\r", "");
        desc.setText(descText);

        return convertView;
    }
}
